<?php

class Domain_App_Project {
    function __construct() {
        $this->_model = new Model_App_Project();
        $this->_apply = new Model_App_Apply();
    }
    /**
     * 根据 pid 查询项目表中的一条数据
     */
    public function getProjectByPid($pid){
        $info = $this->_model->getProjectByPid($pid);
        if($info){
            return $info;
        }
        throw new PhalApi_Exception('PID NOT FOUND', 400);
    }

    /**
     * 根据 admin 查询客户对应的项目
     */

    public function plists($data){
        $info = $this->_model->plists($data);
        if($info){
            return $info;
        }
        throw new PhalApi_Exception('ADMIN NOT FOUND', 400);
    }

    /**
     * 添加一条项目数据
     */
    public function addProject($data){
        //检查 提交的数据 pid 是否重复
        $checkProjectDup = $this->_model->checkProjectDup($data);
        if ($checkProjectDup) throw new PhalApi_Exception('DUPLICATE', 400);

        //检查 apply_id 是否存在
        $checkProjectOpenid = $this->_apply->checkProjectOpenid($data);
        if (!$checkProjectOpenid) throw new PhalApi_Exception('ADMIN NOT FOUND', 400);
        return $this->_model->add($data);
    }

    /**
     * 根据 pid 删除 一条项目资料
     */
    public function deleteProjectByPid($data){
        $info = $this->_model->delete($data);
        if($info){
            return $info;
        }
        throw new PhalApi_Exception('Pid NOT FOUND', 400);
    }
    /**
     * 根据 pid 更新项目资料
     */
    public function editProjectByPid($data,$pid){
        //检查 pid 是否存在
        $checkProjectPid = $this->_model->getProjectByPid($pid);
        if(!$checkProjectPid) throw new PhalApi_Exception('PID NOT FOUND', 400);
        //检查 admin 是否存在
        $checkProjectOpenid = $this->_apply->checkProjectOpenid($data);
        if (!$checkProjectOpenid) throw new PhalApi_Exception('ADMIN NOT FOUND', 400);
        return $this->_model->edit($data,$pid);
    }
}
